﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MyWorkBlogDAL
{
  public  partial class Comment
    {
      public static List<Comment> GetComments(int postId)
      {
          var query = (from c in BlogDB.Comments
                       where c.BlogPostId == postId && c.IsDeleted == false
                       select c).ToList();


          //var query = (BlogDB.Comments
          //    .Where(n => n.BlogPostId == postId && n.IsDeleted == false)).ToList();
          return query;
      }

      public static int InsertComment(Comment comment)
      {
          
          BlogDB.Comments.Add(comment);
          return BlogDB.Save();
      }

      public static int DeleteComment(Comment comment)
      {
          var query = BlogDB.Comments
              .FirstOrDefault((n => n.BlogPostId == comment.BlogPostId && n.CommentId == comment.CommentId));
          query.IsDeleted = true;
          BlogDB.Comments.AddOrUpdate(query);
          return BlogDB.Save();
      }
      //public static int GetPostId(int commentId)
      //{
      //    var query = BlogDB.Comments
      //        .FirstOrDefault(n => n.CommentId == commentId);
      //    return query.BlogPostId;
      //}
      public static Comment GetCommentObject(int commentId)
      {
          var query = BlogDB.Comments
              .FirstOrDefault(n => n.CommentId == commentId && n.IsDeleted == false);
          return query;
      }

      public static int GetCommentPostId(int commentId)
      {
          var query = BlogDB.Comments
              .SingleOrDefault(n => n.CommentId == commentId);
        
          return query.BlogPostId;
      }
    }
}
